﻿/* $RCSFile: InitSubSystem.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Configuration/InitSubSystem.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:27  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Diagnostics;
using System.Globalization;

using AddonNice.Diagnostics;

namespace AddonNice.Configuration
{
    class InitSubSystem : ConfigurationSection
    {
        public static void CreateSection(string productName,string initClass,string loadAssembly)
        {
            if ( GlobalTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]InitSubSystem CreateSection productName: {0}, initClass: {1}, loadAssembly: {2}", productName,initClass,loadAssembly),GlobalTraceSwitch.Sw.Info);
            try
            {

                InitSubSystem customSection;

                // Get the current configuration file.
                System.Configuration.Configuration config       =   WebConfigurationManager.OpenWebConfiguration("~");

                // Create the section entry  
                // in <configSections> and the 
                // related target section in <configuration>.
                // License is the Group->could register several companies, not available yet
                if ( config.Sections["InitSubSystem"] == null )
                {
                    if ( GlobalTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( "[V]InitSubSystem CreateSectionInitClass creating group.",GlobalTraceSwitch.Sw.Info);
                    customSection                               =   new InitSubSystem();
                    InitData lic                                =   new InitData(productName,initClass,loadAssembly);
                    customSection.InitClasses.Add(lic);
                    config.Sections.Add("InitSubSystem",customSection);
                    if ( GlobalTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]InitSubSystem CreateSection name: {0} created", customSection.SectionInformation.Name),GlobalTraceSwitch.Sw.Info);
                }
                else
                {
                    if ( GlobalTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( "[V]InitSubSystem CreateSectionInitClass section found.",GlobalTraceSwitch.Sw.Info);
                    customSection                               =   (InitSubSystem)config.Sections["InitSubSystem"];
                    if ( GlobalTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( "[V]InitSubSystem CreateSectionInitClass section Company found.",GlobalTraceSwitch.Sw.Info);
                    if ( customSection.InitClasses[productName] == null )
                    {
                        InitData lic                           =   new InitData(productName,initClass,loadAssembly);
                        customSection.InitClasses.Add(lic);
                        if ( GlobalTraceSwitch.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]InitSubSystem CreateSectionInitClass added product: {0}, initClass: {1}",productName,initClass),GlobalTraceSwitch.Sw.Info);
                    }
                    else
                    {
                        InitData lic                           =   customSection.InitClasses[productName];
                        if ( GlobalTraceSwitch.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]InitSubSystem CreateSection InitClass found product: {0}, InitClass: {1}",productName,lic.InitClass),GlobalTraceSwitch.Sw.Info);
                        lic.InitClass                           =   initClass;
                        lic.LoadAssembly                        =   loadAssembly;
                        if ( GlobalTraceSwitch.Sw.TraceVerbose )
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]InitSubSystem CreateSectionInitClass modified product: {0}, InitClass: {1}",productName,initClass),GlobalTraceSwitch.Sw.Info);
                    }
                }
                customSection.SectionInformation.ForceSave      =   true;
                config.Save(ConfigurationSaveMode.Full);

                if ( GlobalTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]InitSubSystem CreateSection section saved Name: {0}.", customSection.SectionInformation.Name),GlobalTraceSwitch.Sw.Info);
            }
            catch (Exception ex)
            {
                if ( GlobalTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]InitSubSystem CreateSection ex: {0}", ex),GlobalTraceSwitch.Sw.Info);
                throw;
            }
            if ( GlobalTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]InitSubSystem CreateSection END",GlobalTraceSwitch.Sw.Info);
        }
        
        public static InitSubSystem GetConfig()
        {
            return ConfigurationManager.GetSection("InitSubSystem") as InitSubSystem;
        }

        [ConfigurationProperty("InitClasses")]
        public InitDataCollection InitClasses
        {
            get
            {
                return this["InitClasses"] as InitDataCollection;
            }
        }
   }


    public class InitDataCollection : ConfigurationElementCollection
    {
        public InitData this[int index]
        {
            get
            {
                return base.BaseGet(index) as InitData;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

       new public InitData this[string Name]
        {
            get
            {
                return (InitData)BaseGet(Name);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new InitData();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((InitData)element).Product;
        }

        /**/
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        protected override ConfigurationElement CreateNewElement(string productName)
        {
            return new InitData(productName);
        }

        public new string AddElementName
        {
            get
            { return base.AddElementName; }

            set
            { base.AddElementName = value; }

        }

        public new string ClearElementName
        {
            get
            { return base.ClearElementName; }

            set
            { base.AddElementName = value; }

        }

        public new string RemoveElementName
        {
            get
            { return base.RemoveElementName; }
        }

        public new int Count
        {
            get { return base.Count; }
        }

 
        public int IndexOf(InitData product)
        {
            return BaseIndexOf(product);
        }

        public void Add(InitData product)
        {
            BaseAdd(product);
            // Add custom code here.
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
            // Add custom code here.
        }

        public void Remove(InitData productLic)
        {
            if (BaseIndexOf(productLic) >= 0)
                BaseRemove(productLic.Product);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
            // Add custom code here.
        }
   }

    public class InitData : ConfigurationElement
    {
        [ConfigurationProperty("Product", IsRequired = true, IsKey=true)]
        public string Product
        {
            get
            {
                return this["Product"] as string;
            }
            set
            {
                this["Product"] = value;
            }
        }


        [ConfigurationProperty("initClass", IsRequired = true)]
        public string InitClass
        {
            get
            {
                return this["initClass"] as string;
            }
            set
            {
                this["initClass"] = value;
            }
        }

        [ConfigurationProperty("loadAssembly", IsRequired = true)]
        public string LoadAssembly
        {
            get
            {
                return this["loadAssembly"] as string;
            }
            set
            {
                this["loadAssembly"] = value;
            }
        }

        public InitData()
        {}

        public InitData(string productName)
        {
            Product =   productName;
        }

        public InitData(string productName,string winitClass,string wLoadAssembly)
        {
            Product         =   productName;
            InitClass       =   winitClass;
            LoadAssembly    =   wLoadAssembly;
        }
    }
}
